/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.helper;

import com.ivanbiz.dao.InternalDeliveryDAO;
import com.ivanbiz.dao.LockDateDAO;
import com.ivanbiz.dao.PurchaseOrderDAO;
import com.ivanbiz.dao.impl.InternalDeliveryDAOImpl;
import com.ivanbiz.dao.impl.LockDateDAOImpl;
import com.ivanbiz.dao.impl.PurchaseOrderDAOImpl;
import com.ivanbiz.model.InternalDelivery;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class MyUniqueNumber {

    public String getNoTransaksi() {
        String noTransaksi = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String prev = GlobalSession.getPreviousNoTransaksi();
        if (!"".equals(prev)) {
            String[] splitPrev = prev.split("-");
            int count = Integer.parseInt(splitPrev[3]);
            count += 1;
            noTransaksi = "PO-" + GlobalSession.getNameCompany() + "-" + sdf.format(GlobalSession.getLockDate()) + "-" + count;
        } else {
            noTransaksi = "PO-" + GlobalSession.getNameCompany() + "-" + sdf.format(GlobalSession.getLockDate()) + "-" + "1";
        }
        return noTransaksi;
    }

    public void setDateUniqueNumber() {
        LockDateDAO lockDateDAO = new LockDateDAOImpl();
        PurchaseOrderDAO purchaseOrderDAO = new PurchaseOrderDAOImpl();
        try {
            Date date = lockDateDAO.getDateToday();
            String noTransaksi = purchaseOrderDAO.getLastNoTransaksi();
            GlobalSession.setLockDate(date);
            GlobalSession.setPreviousNoTransaksi(noTransaksi);
        } catch (Exception ex) {
            Logger.getLogger(MyUniqueNumber.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getNoInternalDelivery() {
        String noTransaksi = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String prev = GlobalSession.getPreviousNoInternalDelivery();
        if (!"".equals(prev)) {
            String[] splitPrev = prev.split("-");
            int count = Integer.parseInt(splitPrev[3]);
            count += 1;
            noTransaksi = "ID-" + GlobalSession.getNameCompany() + "-" + sdf.format(GlobalSession.getLockDate()) + "-" + count;
        } else {
            noTransaksi = "ID-" + GlobalSession.getNameCompany() + "-" + sdf.format(GlobalSession.getLockDate()) + "-" + "1";
        }
        return noTransaksi;
    }

    public void setDateUniqueNumberInternalDelivery() {
        LockDateDAO lockDateDAO = new LockDateDAOImpl();
        InternalDeliveryDAO internalDeliveryDAO = new InternalDeliveryDAOImpl();
        try {
            Date date = lockDateDAO.getDateToday();
            String noTransaksi = internalDeliveryDAO.getLastNoTransaksi();
            GlobalSession.setLockDate(date);
            GlobalSession.setPreviousNoInternalDelivery(noTransaksi);
        } catch (Exception ex) {
            Logger.getLogger(MyUniqueNumber.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
